package com.gopay.bis.web.controller.expay;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.gopay.bis.common.util.IpAddrUtil;
import com.gopay.bis.common.util.SendSmsUtils;
import com.gopay.bis.domain.order.OrderInfo;
import com.gopay.bis.service.bank.BankOrderService;
import com.gopay.common.Result;
import com.gopay.common.constants.proccode.ProcCodeConstants;
import com.gopay.common.domain.bank.BankOrder;
import com.gopay.common.domain.bank.ExPayBaseMessage;
import com.gopay.common.util.HttpTransport;
import com.gopay.remote.bank.ExPayInteractRemoteService;

/** 
 * 快捷支付异步退款
 * @ClassName: ExpayRefundBankResBase 
 * @author w_jiqing 
 * @date 2017年8月9日 下午4:32:42  
 */ 
public abstract class ExpayRefundBankResBase extends ExPayBankResBase{
    
    private static Log logger = LogFactory.getLog(ExpayRefundBankResBase.class);
    
    

    @Resource(name = "notifyCashier")
    private HttpTransport notifyCashier;
    
    @Resource(name = "bankOrderService")
    private BankOrderService bankOrderService;
    
    @Resource(name = "smsConfig")
    private Map<String, String> smsConfig;
    
    @Resource(name = "exPaybankInteractService")
    private ExPayInteractRemoteService exPaybankInteractService;
    
    /**
     * 验证返回参数
     * 
     * @param request
     * @return
     */
    protected abstract boolean checkRefundParameters(HttpServletRequest request);
    
    /**
     * 构建退款类对象
     * 
     * @param orderId
     * @return
     */
    protected abstract ExPayBaseMessage buildRefundMessage(HttpServletRequest request);
    
    /**
     * 构建返回收银台参数
     * 
     * @param request
     * @return
     */
    protected abstract OrderInfo buildRefundOrderInfo(HttpServletRequest request,Result result);
    
    /**
     * 退款通知入口
     * 
     * @param request
     * @param response
     * @return
     */
    protected void executeRefund(HttpServletRequest request, HttpServletResponse response) {
        // 打印请求参数
        this.displayRequestParameters(request, logger);
        
        //校验参数
        boolean isCheck = checkRefundParameters(request);
        try{
            if(isCheck){
                OrderInfo orderInfo = null;
                //退款返回处理
                ExPayBaseMessage baseMsg = buildRefundMessage(request);
                Result result = exPaybankInteractService.refundBack(baseMsg);
                
               // 构建发往收银台的参数
                orderInfo = buildRefundOrderInfo(request, result);
                orderInfo.setSign(sign(orderInfo));//  前台通知cashier签名数据
                logger.error(orderInfo.getOrderId() + " Referer: " + request.getHeader("Referer") + " Ip: " + IpAddrUtil.getRemoteRealIpAddr(request));
                if(result.isSuccess()){
                    if((Boolean)result.getDefaultModel()){
                        Map<String, String> param  = (Map<String, String>)result.getModel("isNotifyCashier");
                        param.put("sign", signRes(param));// 后台通知cashier签名数据
                        String gopayOrderID = param.get("orderId");
                        String payStatus = param.get("payStatus"); //交易状态
                        if(("00").equals(payStatus)){                            
                            logger.error("bis expay notifyCashier: " + param);
                            notifyCashier.submit(param);
                        }else{
                            //退款失败不通知收银台
                            logger.error(gopayOrderID + " bis expay fail notifyCashier");
                           // notifyCashier.submit(param);
                        }         
                        // 通知银行
                        if (orderInfo.getIsNotify()) {
                            notifyBank(response, orderInfo);
                        }
                    }else{
                     // 验签失败，不做处理
                        logger.error("平台退款订单[" + orderInfo.getOrderId() + "]验签失败 " + ToStringBuilder.reflectionToString(result));
                    }
                }else {
                    //系统异常，不做处理
                    logger.error("平台退款订单[" + orderInfo.getOrderId() + "]处理失败: " + ToStringBuilder.reflectionToString(result));
                }
            }else{
                //参数校验失败
                logger.error("bank executeRefund notify parameters is null");
            }
        }catch (Exception e) {
            logger.error("ExpayRefundBankResBase executeRefund error：", e);
        }
    }
    
    

}
